package com.yuanyi.liu;

import java.util.ArrayList;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.view.MotionEvent;
import android.view.View;

public class DrawRectView extends View{
	private Paint mPaint;
	private Bitmap mBitmap;
	private float x1 = 0;
	private float y1 = 0;
	private ArrayList<MyRect> rects;
	private boolean isMove = false;
	private boolean changeSize1 = false;
	private boolean changeSize2 = false;
	private int flag;
	public DrawRectView(Context context, int flag) {
		super(context);
		this.flag = flag;
		mPaint = new Paint();
		mPaint.setAntiAlias(true);
		mPaint.setARGB(0, 0xff, 0, 0);
		mPaint.setStyle(Paint.Style.STROKE);
		mBitmap = Bitmap.createBitmap(480, 800, Bitmap.Config.RGB_565);
		rects = new ArrayList<MyRect>();
	}
	
	//自己写的画矩形函数
	private void drawMyRect(MyRect rect, Canvas canvas){
		canvas.drawLine(rect.x1, rect.y1, rect.x2, rect.y1, mPaint);
		canvas.drawLine(rect.x1, rect.y1, rect.x1, rect.y2, mPaint);
		canvas.drawLine(rect.x2, rect.y2, rect.x2, rect.y1, mPaint);
		canvas.drawLine(rect.x2, rect.y2, rect.x1, rect.y2, mPaint);
	}
	
	protected void onDraw(Canvas canvas){
		super.onDraw(canvas);
		if(mBitmap != null)
			canvas.drawBitmap(mBitmap, 0, 0,null);
	}
	
	public boolean onTouchEvent(MotionEvent event){
		int x = 0;
		int y = 0;
		x = (int)event.getX();
		y = (int)event.getY();
		Canvas canvas = new Canvas();
		canvas.setBitmap(mBitmap);
		mPaint.setARGB(200, 0xff, 0, 0);
		
		canvas.drawColor(Color.BLACK);
		switch(event.getAction()){
			case MotionEvent.ACTION_DOWN:{
				//没有直线就创建直线，否则判定是移动还是改变直线大小
				if(rects.size()==0){
					rects.add(new MyRect(x-30, y-30, x+30, y+30));
				}else{
					int mid_x, mid_y;
					for(int i = 0; i < rects.size(); i++){
						MyRect rect = rects.get(i);
						mid_x = (rect.x1 + rect.x2)/2;
						mid_y = (rect.y1 + rect.y2)/2;
						if(x>mid_x-30 && x<mid_x+30
								&&y>mid_y-30 && y<mid_y+30){
							isMove = true;
							break;
						}else if(x>rect.x1-20 && x<rect.x1+20 
								&& y>rect.y1-20 && y<rect.y1+20){
							changeSize1 = true;
							break;
						}else if(x>rect.x2-20 && x<rect.x2+20 
								&& y>rect.y2-20 && y<rect.y2+20){
							changeSize2 = true;
							break;
						}
					}
				}
				break;
			}
			case MotionEvent.ACTION_MOVE:{
				if(rects.size()!=0){
					if(isMove){
						MyRect rect = rects.get(0);
						int mid_x = (rect.x1 + rect.x2)/2;
						int mid_y = (rect.y1 + rect.y2)/2;
						int move_x = x - mid_x;
						int move_y = y - mid_y;
						rect.x1 += move_x;
						rect.x2 += move_x;
						rect.y1 += move_y;
						rect.y2 += move_y;
					}
					
					if(changeSize1){
						MyRect rect = rects.get(0);
						rect.x1 = x;
						rect.y1 = y;
					}
					
					if(changeSize2){
						MyRect rect = rects.get(0);
						rect.x2 = x;
						rect.y2 = y;
						
					}
				}
				break;
			}
			case MotionEvent.ACTION_UP:{
				isMove = false;
				changeSize1 = false;
				changeSize2 = false;
				break;
			}
		}
		
		if(rects.size()!=0){
			//drawMyRect(rects.get(0), canvas);
			MyRect r = rects.get(0);
			switch(flag){
				case 1:{   //画直线
					canvas.drawLine(r.x1, r.y1, r.x2, r.y2, mPaint);
					break;
				}
				case 2:{   //画矩形
					canvas.drawRect(new RectF(r.x1, r.y1, r.x2, r.y2), mPaint);
					break;
				}
				case 3:{   //画椭圆
					canvas.drawOval(new RectF(r.x1, r.y1, r.x2, r.y2), mPaint);
					break;
				}
				default:{
					//invalidate();
				}
			}
			invalidate();
		}
		
		return true;
	}

}

class MyRect{
	
	public MyRect(int x1, int y1, int x2, int y2){
		this.x1 = x1;
		this.x2 = x2;
		this.y1 = y1;
		this.y2 = y2;
	}
	int x1,x2,y1,y2;
}
